
package com.funo.government.info.utils;

import android.text.TextUtils;

import com.funo.government.info.utils.BusinessResolver.BusinessCallback;
import com.funo.government.info.utils.AsyncTask;

@SuppressWarnings("rawtypes")
public class RequestTask extends AsyncTask<BusinessRequest, Void, BusinessResult> {

    private static final String TAG = "RequestTask";

    private BusinessCallback mCallback;

    RequestTask(BusinessCallback callback) {
        mCallback = callback;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected BusinessResult doInBackground(BusinessRequest... params) {
        BusinessRequest request = params[0];
        BusinessResult result = new BusinessResult();
        String resultJson = null;
        try {
            switch (request.requestType) {
                case BusinessRequest.REQUEST_TYPE_GET:
                    if (request.params != null) {
                        resultJson = BusinessResolver.getData(request.url, request.params);
                    } else {
                        resultJson = BusinessResolver.getData(request.url);
                    }
                    break;
                case BusinessRequest.REQUEST_TYPE_POST:
                    resultJson = BusinessResolver.postData(request.url, request.params);
                    break;
            }
            LogUtils.d(TAG, "url:" + request.url + "\nresultJson:" + resultJson);
            if (!TextUtils.isEmpty(resultJson)) {

                switch (request.resultType) {
                    case BusinessRequest.RESULT_TYPE_OBJECT:
                        // 返回对象
                        if (request.cls == null) {
                            LogUtils.e(TAG, "request.cls can't be null");
                            break;
                        }
                        result.returnObject = JsonUtils.objectFromJson(resultJson, request.cls);
                        if (result.returnObject != null) {
                            LogUtils.d(TAG, "returnObject" + result.returnObject.toString());
                        } else {
                            LogUtils.d(TAG, "returnObject:null");
                        }
                        break;
                    case BusinessRequest.RESULT_TYPE_LIST:
                        // 返回集合
                        if (request.cls == null) {
                            LogUtils.e(TAG, "request.cls can't be null");
                            break;
                        }
                        try {
                            result.returnObject = JsonUtils.listFromJson(resultJson, request.cls);
                        } catch (Exception e) {
                            result.success = false;
                            result.exception = new BusinessException(
                                    BusinessException.CODE_ILLEGAL_RETURN);
                            result.returnObject = null;
                            e.printStackTrace();
                        }
                        break;
                    case BusinessRequest.RESULT_TYPE_VOID:
                        result.returnObject = null;
                        break;
                    case BusinessRequest.RESULT_TYPE_CUSTOM:
                        // 自定义接口
                        if (request.parser == null) {
                            LogUtils.e(TAG, "request.parser can't be null");
                            break;
                        }
                        result.returnObject = request.parser.parse(resultJson);
                        break;
                }
            }
            // 返回成功
            result.success = true;

        } catch (BusinessException e) {
            result.exception = e;
            result.returnObject = null;
            result.success = false;
        } catch (Exception e) {
            e.printStackTrace();
            result.returnObject = null;
            result.success = false;
        }
        return result;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected void onPostExecute(BusinessResult result) {
        if (mCallback != null && !isCancelled()) {
            if (result.success) {
                mCallback.onSuccess(result.returnObject);
            } else {
                if (result.exception != null) {
                    mCallback.onError(result.exception);
                }
            }
        }
    }

}
